package com.xiake.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiake.pojo.User;
import com.xiake.service.UserService;
import com.xiake.mapper.UserMapper;
import com.xiake.utils.JwtHelper;
import com.xiake.utils.MD5Util;
import com.xiake.utils.Result;
import com.xiake.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author 周清峰
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-03-12 14:19:43
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
   @Autowired
  UserMapper userMapper;
   @Autowired
    JwtHelper jwtHelper;
   //登录接口
    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //在数据库进行查询当前用户的用户名，判断用户是否存在
        userLambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        User user1 = userMapper.selectOne(userLambdaQueryWrapper);
        //不存在直接返回501
        if(user1==null){
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }

        //用户名存在，则查验密码
        //如果密码正确则直接返回200的json
        //密码不为空并且加密后的密码和数据库中密码一致
        if(!StringUtils.isEmpty(user.getUserPwd())&& MD5Util.encrypt(user.getUserPwd())
                .equals(user1.getUserPwd())){
            //创建一个token
            String token = jwtHelper.createToken(Long.valueOf(user1.getUid()));
            Map<String, String> map = new HashMap<>();
            map.put("token",token);
            return Result.ok(map);
        }
        //如果密码不正确则直接返回503，密码不正确
        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);

    }

    //根据token解析用户信息接口
    @Override
    public Result getUserInfo(String token) {
        //判断用户的token期限是否到期，到期返回未登录状态505
        boolean expiration = jwtHelper.isExpiration(token); //返回值为true代表到期了
        if (expiration){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        //为到期则显示用户的除密码之后的信息
        Long userId = jwtHelper.getUserId(token);
        User user = userMapper.selectById(userId);
        user.setUserPwd("");
        Map<String,User> map = new HashMap<>();
         map.put("loginUser",user);
         return Result.ok(map);
    }
  //发起一个post请求传入一个用户名，检查数据库是否存在
    @Override
    public Result checkUsername(String username) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUsername,username);
        Long count = userMapper.selectCount(userLambdaQueryWrapper);//用于统计满足特定条件的数据数量
        //返回0证明没有找到与此相关的数据条数
        if(count==0){
            return Result.ok(null);
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }
  //实现用户的注册功能
    @Override
    public Result register(User user) {
        //先查询用户名是否有被占用
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        Long count = userMapper.selectCount(userLambdaQueryWrapper);//用于统计满足特定条件的数据数量
        //返回0证明没有找到与此相关的数据条数
        if(count==0){
            String pwd = MD5Util.encrypt(user.getUserPwd());
            user.setUserPwd(pwd);
            int row = userMapper.insert(user);
            if(row==1){
                return Result.build(null,ResultCodeEnum.SUCCESS);
            }
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);

    }

    @Override
    public Result checkLogin(String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if(expiration){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);

    }
}




